package com.ross.web.dao;

import java.util.LinkedList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.AliasToBeanResultTransformer;

import com.ross.web.domain.Category;
import com.ross.web.domain.Orders;
import com.ross.web.util.HibernateUtil;

public class CategoryDAO extends HibernateUtil {
	
	public List getAllCategory(){
		
		Session session = HibernateUtil.getSession();
		List result = new LinkedList();
		
		try{
			
			result = session.createCriteria(Category.class, "category")
				.setProjection(Projections.projectionList()
						.add(Projections.property("category.c_id"),"c_id")
						.add(Projections.property("category.c_name"),"c_name")
						.add(Projections.property("category.c_description"),"c_description")
						)
						
						.setResultTransformer(new AliasToBeanResultTransformer(Category.class))
						.list();
			
			
			
		return result;
			
		}finally {
			session.close();
		}		
	}
	
	public Category getAllCategoryById(int c_id){
		
		Session session = HibernateUtil.getSession();
		List result = new LinkedList();
		
		try{
			
			result = session.createCriteria(Category.class, "category")
				.setProjection(Projections.projectionList()
						.add(Projections.property("category.c_id"),"c_id")
						.add(Projections.property("category.c_name"),"c_name")
						.add(Projections.property("category.c_description"),"c_description")
						)
						
						.add(Restrictions.eq("category.c_id", c_id))
						
						.setResultTransformer(new AliasToBeanResultTransformer(Category.class))
						.list();
			
			
			
		return result.isEmpty() ? null : (Category) result.get(0);
			
		}finally {
			session.close();
		}		
	}
	
	public Category getCategoryByName(String c_name){
		
		Session session = HibernateUtil.getSession();
		List result = new LinkedList();
		
		try{
			
			result = session.createCriteria(Category.class, "category")
				.setProjection(Projections.projectionList()
						.add(Projections.property("category.c_id"),"c_id")
						.add(Projections.property("category.c_name"),"c_name")
						.add(Projections.property("category.c_description"),"c_description")
						)
						
						.add(Restrictions.eq("category.c_name", c_name))
						
						.setResultTransformer(new AliasToBeanResultTransformer(Category.class))
						.list();
			
			
			
		return result.isEmpty() ? null : (Category) result.get(0);
			
		}finally {
			session.close();
		}		
	}
	
	public void insert(Category category){
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		
		try{
			tx = session.beginTransaction();
			session.save(category);
			tx.commit();
		}catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}
		
	}
	
	public void update(Category category){
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		
		try{
			tx = session.beginTransaction();
			session.update(category);
			tx.commit();
		}catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}
		
	}
	
	public void delete(Category category){
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		
		try{
			tx = session.beginTransaction();
			session.delete(category);
			tx.commit();
		}catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}
		
	}

}
